<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增代理商扣款规则')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-agentDeductions-add" enctype="multipart/form-data">
        <div class="form-group">
            <label class="col-sm-3 control-label">服务商编号：</label>
            <div class="col-sm-8">
                <input name="agentNum" class="form-control" id="xagentNum" type="text" readonly>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">服务商名称：</label>
            <div class="col-sm-8">
                <input name="agentName" onclick="selectAgent()"  class="form-control" id="xagentName" type="text" required readonly>
                <i class="fa fa-search" style="margin-top: -22px;margin-right:3px;float:right;"></i>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">扣款类型 ：</label>
            <div class="col-sm-8">
                <select name="deductionType" onchange="terminalShow(this)" class="form-control m-b">
                    <option value="2">伪激活</option>
                    <option value="1">机具扣款</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">机具类型：</label>
            <div class="col-sm-8">
                <select id="posType" name="posType" class="form-control noselect2 selectpicker" th:with="type=${@dict.getType('terminal_type')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                </select>
            </div>
        </div>
        <div id="terminalShow">
            <div class="form-group">
                <label class="col-sm-3 control-label">划拨类型：</label>
                <div class="col-sm-6" style="margin-top: 5px;">
                    <label style="text-align: center"> <input type="radio" checked onchange="javascript:window.onchangeRadio(this)" name="hbType" value="1"/> SN编号 </label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <label style="text-align: center"> <input type="radio" onchange="javascript:window.onchangeRadio(this)" name="hbType" value="2"/> 文件导入</label>
                </div>
            </div>
            <div class="form-group" id="machine">
                <label class="col-sm-3 control-label">sn号：</label>
                <div class="col-sm-8">
                    <input name="sn" id="sn" class="form-control" onclick="selectPos()" type="text" required readonly>
                    <i class="fa fa-search" style="margin-top: -22px;margin-right:3px;float:right;"></i>
                </div>
            </div>
            <div class="form-group" id="excel" style="display: none">
                <label class="col-sm-3 control-label is-required">终端文件：</label>
                <div class="col-sm-8">
                    <span class="btn btn-white btn-file">
                        <input id="files" type="file" name="file"  accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" />
                    </span>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">机具数量：</label>
                <div class="col-sm-8">
                    <input name="posNumber" id="count" class="form-control" type="text" readonly>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">扣款金额（<span id="dan">单</span>元）：</label>
            <div class="col-sm-8">
                <input name="deductionMoney" class="form-control" type="text" id="deductionMoney" oninput="value=value.replace(/^(0+)|[^\d]+/g,'')">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">扣款比例（%）：</label>
            <div class="col-sm-8">
                <input name="ratio" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">扣款用途：</label>
            <div class="col-sm-8">
                <input name="note" class="form-control" id="note" type="text" required>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: use-js"/>
<th:block th:include="include :: datetimepicker-js" />
<script th:inline="javascript">
    var prefix1 = ctx + "terminal/record"
    var prefix = ctx + "finance/agentDeductions"
    $("#form-agentDeductions-add").validate({
        focusCleanup: true
    });

    $("input[name='date']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    function submitHandler() {
        if($("#deductionMoney").val() <= 0){
            $.modal.alertWarning("扣款金额必须大于0！");
            return;
        }
        if (!$.validate.form()) {
            return;
        }
        var formData = new FormData();
        formData.append("agentNum",$("[name='agentNum']").val());     //目标代理商编号
        formData.append("agentName",$("[name='agentName']").val());   //目标代理商名称
        formData.append("deductionType",$("[name='deductionType']").val());                 //划拨数量
        formData.append("posType",$("[name='posType']").val());                //终端类型
        formData.append("sn",$("[name='sn']").val());                 //活动
        formData.append("posNumber",$("[name='posNumber']").val());             //到期时间
        formData.append("deductionMoney",$("[name='deductionMoney']").val());       //是否分配档位 -1 不分配 1 分配
        formData.append("ratio",$("[name='ratio']").val());       //是否分配档位 -1 不分配 1 分配
        formData.append("note",$("[name='note']").val());       //是否分配档位 -1 不分配 1 分配

        formData.append("file", $("[name = 'file']")[0].files[0]);


        $.ajax({
            url : prefix + "/add",
            type: 'POST',
            data: formData,
            cache: false,
            contentType: false,
            processData: false,
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
                $.modal.disable();
            },
            success : function(result) {
                if (result.code == web_status.SUCCESS) {
                    var parent = window.parent;
                    if (parent.table.options.type == table_type.bootstrapTable) {
                        $.modal.close();
                        parent.$.modal.msgSuccess(result.msg);
                        parent.$.table.refresh();
                    } else if (parent.table.options.type == table_type.bootstrapTreeTable) {
                        $.modal.close();
                        parent.$.modal.msgSuccess(result.msg);
                        parent.$.treeTable.refresh();
                    } else {
                        $.modal.msgReload("保存成功,正在刷新数据请稍后……", modal_status.SUCCESS);
                    }
                } else if (result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg)
                }  else {
                    $.modal.alertError(result.msg);
                }
                $.modal.closeLoading();
                $.modal.enable();
                //$.modal.alertSuccess(data.msg)
            },
            error : function(result) {
                $.modal.alertError(result.msg);
            }
        });
    }
    function selectAgent() {
        // var url =ctx + "terminal/record"+ "/selectAgentInfo";
        var url =prefix + "/selectAgentInfo2";
        $.modal.open("选择服务商" , url, '700');
    }

    function selectPos() {
        if($("#xagentNum").val() == ""){
            $.modal.alertWarning("请先填写服务商！");
            return;
        }
        var url =prefix+ "/selectPosInfo?pos_type="+$("#posType").val()+"&agent_num="+$("#xagentNum").val()+"&sns="+$("#sn").val()
        $.modal.open("选择sn机具" , url, '700');
    }

    function terminalShow(obj) {
        if(obj.value == 1){
            $("#terminalShow").hide();
            $("#dan").text("总")
            $("#note").val("机具尾款")
            $("#deductionMoney").val("")
            $("#deductionMoney").removeAttrs("readonly")
        }else {
            $("#terminalShow").show();
            $("#dan").text("单")
            $("#note").val("伪激活机具尾款")
            $("#deductionMoney").val("120")
            $("#deductionMoney").attr("readonly","readonly")
        }
    }
    if($("#deductionType").val() == 1){
        $("#note").val("机具尾款")
        $("#deductionMoney").val("")
        $("#deductionMoney").removeAttrs("readonly")
    }else{
        $("#note").val("伪激活机具尾款")
        $("#deductionMoney").val("120")
        $("#deductionMoney").attr("readonly","readonly")
    }

    /**
     * radio的监听操作
     * */
    function onchangeRadio(obj) {
        if (obj.checked) {
            //sn
            if (obj.value == '1') {
                $("#machine").show();
                $("#excel").hide();
                $("#count").attr('readonly',false);
            }
            //文件
            if (obj.value == '2') {
                $("#excel").show();
                $("#machine").hide();
                $("#count").attr('readonly',true);
            }
        }
    }

    /**
     * 上传文件成功后触发的事件
     * result data 读取文件划拨台数
     */
    $("#files").change(function (e){
        var fileData = new FormData();
        if ($("[name = 'file']")[0].files[0]==undefined){
            $("#count").val('');
            return;
        }else{
            fileData.append("file", $("[name = 'file']")[0].files[0]);
            $.ajax({
                url : prefix1 + "/file",
                type: 'POST',
                data: fileData,
                cache: false,
                contentType: false,
                processData: false,
                success : function(result) {
                    $("#count").val(result.data);
                },
                error : function(result) {
                    $.modal.alertError(result.msg);
                }
            })
        }
    })

</script>
</body>
</html>